package com.example.customizedgroupmessage;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.method.PasswordTransformationMethod;
import android.view.*;
import android.widget.*;

public class Configure extends Activity{
	public static final int REQUEST_CODE_CONFIGURE=4001;
	DBHelper mHelper;
	SQLiteDatabase db;
	private EditText emailAddress;
	private EditText password;
	public static Context mContext;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configure);

		Button btnModify = (Button)findViewById(R.id.btnModify);
		btnModify.setOnClickListener(
				new Button.OnClickListener() {
					@Override
					public void onClick(View v) {  
						AlertDialog dialog = createDialogBox();
						dialog.show();

					}
				}
				);


		mHelper = new DBHelper(getBaseContext());
		db = mHelper.getWritableDatabase();
		Cursor cursor;
		String add=null;
		String pass=null;
		cursor = db.rawQuery("SELECT mail, password FROM configure", null);
		while (cursor.moveToNext()) {			
			add=cursor.getString(0);
			pass=cursor.getString(1);}
		
		TextView txtMail =(TextView)findViewById(R.id.currentMail);
		TextView txtPassword =(TextView)findViewById(R.id.currentPass);

		if(add==null || add.length()==0){
			txtMail.setText("아래 버튼을 눌러 G메일 주소를 입력해주세요.");
		}
		else txtMail.setText(add);

		if(pass==null || pass.length()==0){
			txtPassword.setText("아래 버튼을 눌러 G메일 비밀번호를 입력해주세요.");
		}
		else{ txtPassword.setText(pass);		
		PasswordTransformationMethod passwdtm = new PasswordTransformationMethod();
		txtPassword.setTransformationMethod(passwdtm);
		//password.setText(pass);		
		}
		cursor.close();	
		mHelper.close();

	}

	private AlertDialog createDialogBox(){
		View layout = View.inflate(getBaseContext(), R.layout.configure_dialog, null);  
		emailAddress = (EditText) layout.findViewById(R.id.mailEdit);
		password = (EditText) layout.findViewById(R.id.passEdit);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);



		builder.setTitle("email 수정 및 저장");
		builder.setView(layout);

		builder.setNegativeButton("저장", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String email = emailAddress.getText().toString();
				String passw = password.getText().toString();

				mHelper = new DBHelper(getBaseContext());
				db = mHelper.getWritableDatabase();
				Cursor cursor;
				cursor = db.rawQuery("SELECT mail, password FROM configure", null);

				db.execSQL("DELETE FROM configure;");
				db.execSQL("INSERT INTO configure VALUES ('"+email+"','"+passw+"');");
				//	db.execSQL("UPDATE configure SET mail ='"+email+"';");


				cursor.close();	
				mHelper.close();

				if(emailAddress.getText().toString().length()==0){
					TextView txtMail =(TextView)findViewById(R.id.currentMail);
					txtMail.setText("아래 버튼을 눌러 G메일 주소를 입력해주세요.");

				}
				else {Toast.makeText(getBaseContext(), "메일주소가 저장되었습니다.", Toast.LENGTH_SHORT).show();}

				if(password.getText().toString().length()==0){
					TextView txtPassword =(TextView)findViewById(R.id.currentPass);
					txtPassword.setText("아래 버튼을 눌러 G메일 비밀번호를 입력해주세요.");

				}
				else {Toast.makeText(getBaseContext(), "비밀번호가 저장되었습니다.", Toast.LENGTH_SHORT).show();}

				Configure.this.finish();
				Intent intent = new Intent(getBaseContext(), Configure.class);
				startActivityForResult(intent, REQUEST_CODE_CONFIGURE);

			}
		});

		AlertDialog dialog = builder.create();

		return dialog;
	}

}
